package com.situ.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.situ.context.BaseContext;
import com.situ.domain.entity.TCart;
import com.situ.domain.entity.TProduct;
import com.situ.domain.dto.CartDTO;
import com.situ.domain.vo.CartListVO;
import com.situ.ex.CartException;
import com.situ.mapper.TCartMapper;
import com.situ.service.CartService;
import com.situ.service.ProductService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@RequiredArgsConstructor
public class CartServiceImpl extends ServiceImpl<TCartMapper, TCart> implements CartService {


    private final ProductService productService;


    private final TCartMapper cartMapper;

    /**
     * 添加购物车
     * @param cartDTO
     */
    @Override
    @Transactional
    public void addItem(CartDTO cartDTO) {
        TProduct product = productService.getById(cartDTO.getItemId());
        TCart one = this.lambdaQuery()
                .eq(TCart::getUid, BaseContext.getCurrentId())
                .eq(TCart::getPid, cartDTO.getItemId())
                .one();
        TCart cart = TCart.builder()
                .uid(BaseContext.getCurrentId())
                .pid(cartDTO.getItemId())
                .price(product.getPrice())
                .build();
        if (cartDTO.getNum() < 0) {
            if (one == null) {
                throw new CartException("商品不存在");
            }
            if (one.getNum()  <= -cartDTO.getNum()) {
                throw new CartException("商品数量不足");
            }
        }

        if (one != null) {
            cart.setNum(cartDTO.getNum() + one.getNum());
            cart.setModifiedTime(new Date());
            cart.setModifiedUser(BaseContext.getCurrentId().toString());
            this.lambdaUpdate()
                    .eq(TCart::getUid, BaseContext.getCurrentId())
                    .eq(TCart::getPid, cartDTO.getItemId())
                    .update(cart);
        }else {
            cart.setNum(cartDTO.getNum());
            cart.setCreatedTime(new Date());
            cart.setCreatedUser(BaseContext.getCurrentId().toString());
            this.save(cart);
        }
    }

    /**
     * 查询购物车列表
     * @return
     */
    @Override
    public List<CartListVO> getCartListByUserId(Integer userId) {
        return cartMapper.getCartList(userId,null);
    }

    /**
     * 修改购物车数量
     * @param cartDTO
     */
    @Override
    public void changeNum(CartDTO cartDTO) {
        TCart cart = this.lambdaQuery()
                .eq(TCart::getUid, BaseContext.getCurrentId())
                .eq(TCart::getPid, cartDTO.getItemId())
                .one();
        if (cart == null) {
            throw new CartException("商品不存在");
        }
        if (cartDTO.getNum() < 0 && cart.getNum() <= - cartDTO.getNum()) {
            throw new CartException("商品数量不能小于1");
        }
        cart.setNum(cart.getNum() + cartDTO.getNum());
        cart.setModifiedTime(new Date());
        cart.setModifiedUser(BaseContext.getCurrentId().toString());
        this.lambdaUpdate()
                .eq(TCart::getCid,cart.getCid())
                .update(cart);
    }


    /**
     * 删除购物车商品
     * @param itemId
     */
    @Override
    public void deleteItem(Integer itemId) {
        boolean remove = this.lambdaUpdate()
                .eq(TCart::getPid, itemId)
                .eq(TCart::getUid, BaseContext.getCurrentId())
                .remove();
        if (!remove) {
            throw new CartException("删除失败");
        }
    }

    @Override
    public List<CartListVO> getOrderListByIds(List<Integer> ids) {
        return cartMapper.getCartList(BaseContext.getCurrentId(), ids);
    }
}
